/*
 * Copyright (C) 2011-2012 Adam Gregoire <bsdunx@gmail.com>
 * Copyright (C) 2011-2012 Jeff Hubbard <lord2800@gmail.com>
 *
 * This file is part of the ScriptKit scripting engine library.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <cstdarg>

#include "config.hpp"

#if defined _DEBUG && defined OS_WIN32 && defined WANT_CRT_DEBUG
#include "CrtDebug.hpp"

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

#define new DEBUG_NEW
#define malloc(x) MALLOC_DBG(x)

#endif

#include "Exports.hpp"

#include "jsapi.h"
#include "jsapi_ext.hpp"

// TODO Convert to c++ string
JSBool JS_ThrowError(JSContext* cx, const char* message, ...)
{
	va_list args;
	va_start(args, message);

#ifdef OS_WIN32
	const int len = _vscprintf(message, args) + 1;
#else
	const int len = vsnprintf(nullptr, 0, message, args) + 1;
#endif
	char* msg = new char[len];
	memset(msg, 0, len);

#ifdef OS_WIN32
	vsprintf_s(msg, len, message, args);
#else
	vsnprintf(msg, len, message, args);
#endif

	va_end(args);

	JS_ReportError(cx, msg);
	delete[] msg;
	return JS_FALSE;
}

JSObject* JS_NewObjectWithProto(JSContext* cx, JSObject* global, JSClassSpec* classp, JSClassSpec* proto, JSObject* parent)
{
	//JSObject* obj = JS_NewObject(cx, classp->classp, JS_GetProtoForClass(cx, proto->classp), parent);

//	if(proto->static_methods != nullptr) JS_DefineFunctions(cx, obj, proto->static_methods);
//	if(proto->static_properties != nullptr) JS_DefineProperties(cx, obj, proto->static_properties);
//	if(classp->static_methods != nullptr) JS_DefineFunctions(cx, obj, classp->static_methods);
//	if(classp->static_properties != nullptr) JS_DefineProperties(cx, obj, classp->static_properties);

//	if(proto->methods != nullptr) JS_DefineFunctions(cx, obj, proto->methods);
//	if(proto->properties != nullptr) JS_DefineProperties(cx, obj, proto->properties);
//	if(classp->methods != nullptr) JS_DefineFunctions(cx, obj, classp->methods);
//	if(classp->properties != nullptr) JS_DefineProperties(cx, obj, classp->properties);

	return nullptr;
	//return obj;
}
